﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ForumDB
{
	public class PostQuery : Query
	{
		System.Exception errorP = new System.Exception("\n\n**OUT OF RANGE**\n");
		IQueryable<Post> _posts;

		public PostQuery(IQueryable query)
		{
			_posts = query.Cast<Post>();
		}

		public void setTypeQuery(IQueryable query)
		{
			_posts = query.Cast<Post>();
		}

		public void getInfo()
		{
			foreach (var post in _posts)
			{
				Console.WriteLine("ID={0}, Description={1}, Time={2}", post.PostID, post.Description, post.Time);
			}
		}





		public String getInfo2()
		{
			String info = "";
			foreach (var post in _posts)
			{
				info = info + "<br/>" + post.PostID + ", " + post.Description + ", "
				+ post._ThreadID + ", " + post.Title + ", " + post.Body + ", " + post._ClientID + ", " + post.Time;
			}
			return info;
		}




		public String getInfo3()
		{
			foreach (var post in _posts)
			{
				Console.WriteLine("ID={0}, Description={1}, Time={2}", post.PostID, post.Description, post.Time);
			}

			return "ff";

		}



//*****************************************************************************************************
//*****************************************************************************************************

		public override String getPostInfo()
		{
			String info = "";
			foreach (var post in _posts)
			{
				info = info + "<br/>" + post.PostID + ", " + post.Description + ", "
				+ post._ThreadID + ", " + post.Title + ", " + post.Body + ", " + post._ClientID + ", " + post.Time;
			}
			return info;
		}
//starting from here:
//*********************** shared in all objects **********************************************************************

		public override int getId(int numberInTable)
		{
			Post tmpPost = getValidLine(numberInTable);
			if (tmpPost != null)
				return tmpPost.PostID;
			throw errorP;
		}


		public override String getTitle(int numberInTable)
		{
			Post tmpPost = getValidLine(numberInTable);
			if (tmpPost != null)
				return tmpPost.Title;
			throw errorP;
		}


//helper methods

		public Post getValidLine(int numberInTable)
		{
			if (_posts.Count() != 0 && numberInTable <= _posts.Count())
				return _posts.Skip(numberInTable - 1).Take(1).Single();
			return null;
		}




	}
}
